package com.weixing.mall.push;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName GtUtil
 * @Description
 * @Author Small
 * @Date 2021/1/21 16:46
 * @Version 1.0
 **/

public class GtUtil {


    /**
     * 获取服务端token
     *
     * @return
     */
    public static String getFwToken() {
        return getToken(GtConst.F_APP_KEY, GtConst.F_MASTER_SECRET, GtConst.F_AUTH_TOKEN_URL);
    }

    /**
     * 获取代理端token
     *
     * @return
     */
    public static String getDlToken() {
        return getToken(GtConst.APP_KEY, GtConst.MASTER_SECRET, GtConst.AUTH_TOKEN_URL);
    }


    /**
     * 获取token
     *
     * @return
     */
    public static String getToken(String appKey, String masterSecret, String url) {
        long timestamp = System.currentTimeMillis();
        String raw = appKey + timestamp + masterSecret;
        String sign = SecureUtil.sha256(raw);
        Map<String, String> param = Maps.newHashMap();
        param.put("sign", sign);
        param.put("timestamp", timestamp + "");
        param.put("appkey", appKey);
        HttpRequest httpRequest = HttpUtil.createPost(url).body(JSON.toJSONString(param));
        HttpResponse response = httpRequest.execute();
        String body = response.body();
        JSONObject jsonObject = JSON.parseObject(body);
        int code = jsonObject.getIntValue(GtConst.code);
        String token = null;
        if (code == 0) {
            token = jsonObject.getJSONObject("data").getString("token");
        }
        return token;
    }


    /**
     * 绑定别名
     *
     * @param aliasList
     */
    public static void bindAlias(List<CidAlias> aliasList, String token, String url) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("data_list", aliasList);
        System.out.println(JSON.toJSONString(param));
        HttpRequest httpRequest = HttpUtil.createPost(url)
                .header("token", token)
                .contentType("application/json;charset=utf-8")
                .body(JSON.toJSONString(param));
        HttpResponse response = httpRequest.execute();
        String body = response.body();
        System.out.println(body);
    }


    /**
     * 全推
     *
     * @param pushMsg
     * @param token
     */
    public static void pushAll(PushMsg pushMsg, String token, String url) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("request_id", System.currentTimeMillis() + "" + RandomUtil.randomNumbers(6));
        param.put("audience", "all");
        Map<String, Object> pushmessage = Maps.newHashMap();
        pushmessage.put("notification", pushMsg);
        param.put("push_message", pushmessage);
        System.out.println(JSON.toJSONString(param));
        HttpRequest httpRequest = HttpUtil.createPost(url)
                .header("token", token)
                .contentType("application/json;charset=utf-8")
                .body(JSON.toJSONString(param));
        HttpResponse response = httpRequest.execute();
        String body = response.body();
        System.out.println(body);
    }


    /**
     * 向单个用户推送消息，可根据cid指定用户
     *
     * @param pushMsg
     * @param token
     */
    public static void pushToSingleCid(PushMsg pushMsg, String cid, String token, String url) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("request_id", System.currentTimeMillis() + "" + RandomUtil.randomNumbers(6));
        List<String> cids = Lists.newArrayList();
        cids.add(cid);
        Map<String, Object> cidsMap = Maps.newHashMap();
        cidsMap.put("alias", cids);
        param.put("audience", cidsMap);
        Map<String, Object> pushmessage = Maps.newHashMap();
        pushmessage.put("notification", pushMsg);
        param.put("push_message", pushmessage);
        System.out.println(JSON.toJSONString(param));
        HttpRequest httpRequest = HttpUtil.createPost(url)
                .header("token", token)
                .contentType("application/json;charset=utf-8")
                .body(JSON.toJSONString(param));
        HttpResponse response = httpRequest.execute();
        String body = response.body();
        System.out.println(body);
    }


    /**
     * 创建消息
     *
     * @param pushMsg
     * @param token
     */
    public static String listMessage(PushMsg pushMsg, String token, String url) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("request_id", System.currentTimeMillis() + "" + RandomUtil.randomNumbers(6));

        Map<String, Object> pushmessage = Maps.newHashMap();
        pushmessage.put("notification", pushMsg);
        param.put("push_message", pushmessage);
        System.out.println(JSON.toJSONString(param));
        HttpRequest httpRequest = HttpUtil.createPost(url)
                .header("token", token)
                .contentType("application/json;charset=utf-8")
                .body(JSON.toJSONString(param));
        HttpResponse response = httpRequest.execute();
        String body = response.body();
        System.out.println(body);
        JSONObject jsonObject = JSON.parseObject(body);
        return jsonObject.getJSONObject("data").getString("taskid");
    }

    /**
     * 批量推消息
     *
     * @param
     * @param token
     */
    public static void listCid(List<String> cids, String taskid, String token, String url) {
        Map<String, Object> param = Maps.newHashMap();
        param.put("request_id", System.currentTimeMillis() + "" + RandomUtil.randomNumbers(6));

        Map<String, Object> cidsMap = Maps.newHashMap();
        cidsMap.put("alias", cids);
        param.put("audience", cidsMap);
        param.put("taskid", taskid);
        System.out.println(JSON.toJSONString(param));
        HttpRequest httpRequest = HttpUtil.createPost(url)
                .header("token", token)
                .contentType("application/json;charset=utf-8")
                .body(JSON.toJSONString(param));
        HttpResponse response = httpRequest.execute();
        String body = response.body();
        System.out.println(body);
    }


    public static void main(String[] args) {
        // 获得token
        String token = getFwToken();
        String dltoken = getDlToken();
        System.out.println(token);
        System.out.println(dltoken);
//        System.out.println(token);
        // 绑定别名
//        List<CidAlias> aliasList = Lists.newArrayList();
//        CidAlias a1 = new CidAlias("1234", "1234");
//        aliasList.add(a1);
//        bindAlias(aliasList, token);

        // 发送全部
//        PushMsg pushMsg = new PushMsg();
//        pushMsg.setTitle("测试服务端单推标题");
//        pushMsg.setBody("stupid 微星");
//        pushMsg.setClick_type("none");
//        pushToSingleCid(pushMsg,"13111111111", token,GtConst.F_PUSH_TOSINGLECID_URL);


//        List list = new ArrayList<>();
//        for (int i = 1; i <= 1260; i++) {
//            list.add(i);
//        }
//        groupList(list);


    }


    public static void groupList(List list) {
        int listSize = list.size();
        int toIndex = 200;
        int keyToken = 0;
        for (int i = 0; i < list.size(); i += 200) {
            if (i + 200 > listSize) {        //作用为toIndex最后没有100条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List newList = list.subList(i, i + toIndex);
            System.out.println(newList.size());
            System.out.println(newList);
            keyToken++;
        }
    }
}
